package com.ruoyi.project.purchase.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.framework.web.domain.ResultBean;
import com.ruoyi.project.purchase.domain.RyPurchaseOrder;
import com.ruoyi.project.purchase.domain.pojo.DepotDetailBean;
import com.ruoyi.project.purchase.domain.pojo.DepotHeadItem;
import com.ruoyi.project.purchase.domain.pojo.InventoryData;
import com.ruoyi.project.purchase.domain.pojo.ParamBean;
import com.ruoyi.project.purchase.service.IRyQualityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.purchase.domain.RyDepotHead;
import com.ruoyi.project.purchase.service.IRyDepotHeadService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 单据主表Controller
 * 项目总清单
 * @author you
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/purchase/ryDepotHead")
public class RyDepotHeadController extends BaseController
{
    @Autowired
    private IRyDepotHeadService ryDepotHeadService;

    @Autowired
    private IRyQualityService ryQualityService;

    /**
     * 查询项目总清单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryDepotHead:list')")
    @GetMapping("/list")
    public TableDataInfo list(RyDepotHead ryDepotHead)
    {
        startPage();
        ryDepotHead.setType("其他");
        ryDepotHead.setSubType("项目总清单");
        List<RyDepotHead> list = ryDepotHeadService.selectRyDepotHeadList(ryDepotHead);
        return getDataTable(list);
    }

    /**
     * 查询所有可用项目总清单
     */
    @GetMapping("/all")
    public AjaxResult allUsed()
    {
        RyDepotHead ryDepotHead = new RyDepotHead();
        ryDepotHead.setType("其他");
        ryDepotHead.setSubType("项目总清单");
        ryDepotHead.setProjectStatus("0");//查询所有可用的项目总清单,0代表“进行中”的项目
        List<RyDepotHead> list = ryDepotHeadService.selectRyDepotHeadList(ryDepotHead);
//        //返回给前台数据
//        List<JSONObject> resList = new ArrayList<JSONObject>();
//        for(RyDepotHead ryDepotHead1 : list){
//            JSONObject json = new JSONObject();
//            json.put("value",ryDepotHead1.getId());
//            json.put("label",ryDepotHead1.getProjectName());
//            resList.add(json);
//        }
        return AjaxResult.success(list);
    }

    /**
     * 查询所有可用项目总清单
     */
    @GetMapping("/allList")
    public AjaxResult allList()
    {
        RyDepotHead ryDepotHead = new RyDepotHead();
        ryDepotHead.setType("其他");
        ryDepotHead.setSubType("项目总清单");
        List<RyDepotHead> list = ryDepotHeadService.selectRyDepotHeadList(ryDepotHead);
        return AjaxResult.success(list);
    }

    @GetMapping("/dataProcess")
    public void dataProcess(List<InventoryData> dataList)
    {
        System.out.println("dataList:"+dataList);

    }


    /**
     *
     * --项目总清单
     * 获取所有可关联项目总清单信息（项目物品出库使用）
     * 首先查询出项目物品出库单关联的项目总清单id,这些项目总清单id排除后的采项目总清单才是可用的
     *
     */
    @GetMapping("/allForPurchaseOut")
    public AjaxResult allForPurchaseOut(){
        //首先查询出所有质检单
        RyDepotHead ryDepotHead = new RyDepotHead();
        ryDepotHead.setType("出库");
        ryDepotHead.setSubType("项目物品");
        List<RyDepotHead> list = ryDepotHeadService.selectRyDepotHeadList(ryDepotHead);
        //需要排除的id列表
        List<Long> paramList =  new ArrayList<Long>();
        //项目物品出库单不存在，查询所有
        if(list.size() <= 0 || list == null){
            paramList = null;
        } else {
            //遍历结果，筛选出关联的项目总清单id
            for(RyDepotHead ryDepotHead1 : list){
                if(ryDepotHead1.getLinkHeadId() != null){
                    paramList.add(ryDepotHead1.getLinkHeadId());
                }
            }
            //如果没有id需要排除则赋值为null,否则数据库会报错
            if(paramList.size() <= 0){
                paramList = null;
            }
        }
        //根据条件筛选可关联的采购订单
        List<RyDepotHead> resList = ryDepotHeadService.allUsed(paramList);
        return AjaxResult.success(resList);
    }



    /**
     * 导出单据主表列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryDepotHead:export')")
    @Log(title = "单据主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RyDepotHead ryDepotHead)
    {
        List<RyDepotHead> list = ryDepotHeadService.selectRyDepotHeadList(ryDepotHead);
        ExcelUtil<RyDepotHead> util = new ExcelUtil<RyDepotHead>(RyDepotHead.class);
        util.exportExcel(response, list, "单据主表数据");
    }

    /**
     * 获取单据主表详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryDepotHead:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryDepotHeadService.selectRyDepotHeadById(id));
    }


    /**
     * --项目总清单
     * 新增项目总清单及项目总清单子表信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDepotHeadAndDetail")
    public AjaxResult addDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
        System.out.println("depotHeadItem---->" + depotHeadItem);
        return ryDepotHeadService.addDepotHeadAndDetail(depotHeadItem);
    }


    /**
     * --项目总清单
     * 获取项目总清单详细信息
     */
    @GetMapping(value = "/selectByDepotId/{id}")
    public AjaxResult selectByDepotId(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryDepotHeadService.selectRyDepotDetailByDepotId(id));
    }


    /**
     * --项目总清单
     * 修改项目总清单
     */
    @RequestMapping("/editDepotHeadAndDetail")
    public AjaxResult editDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
        System.out.println("depotHeadItem---->" + depotHeadItem);
        return ryDepotHeadService.editDepotHeadAndDetail(depotHeadItem);
    }


    /**
     * 查询可以生成订单的项目明细数据：就是purchase_order_id为空的
     */
    @GetMapping("/selectDetailForOrder")
    public AjaxResult selectDetailForOrder(ParamBean paramBean){
//        if(paramBean.getLinkHeadId() == null){//项目总清单id
//            return error("关联项目不能为空");
//        }
        return AjaxResult.success(ryDepotHeadService.selectDetailForOrder(paramBean));
    }

    @GetMapping("/allForQualityOrder")
    public AjaxResult allForQualityOrder(){
        RyDepotHead ryDepotHead = new RyDepotHead();
        ryDepotHead.setType("其他");
        ryDepotHead.setSubType("采购订单");
        ryDepotHead.setStatus("1");//查询所有可用采购质检,1代表“已审核”的单据
        List<RyDepotHead> list = ryDepotHeadService.selectRyDepotHeadList(ryDepotHead);
        //遍历已审核采购订单，根据id查询是否被采购质检单关联
        List<RyDepotHead> newList = new ArrayList<RyDepotHead>();
        for(RyDepotHead one : list){
            //根据id查询是否被采购质检单关联
            int res = ryQualityService.selectCountByOrderId(one.getId());
            if(res <= 0){//如果订单id未被关联则放入到新数组中返回到前端
                newList.add(one);
            }
        }

        return AjaxResult.success(newList);
    }

    @GetMapping("/allForQuality")
    public AjaxResult allForQuality(){
        RyDepotHead ryDepotHead = new RyDepotHead();
        ryDepotHead.setType("其他");
        ryDepotHead.setSubType("采购订单");
        ryDepotHead.setStatus("1");//查询所有可用采购质检,1代表“已审核”的单据
        List<RyDepotHead> list = ryDepotHeadService.selectRyDepotHeadList(ryDepotHead);
        return AjaxResult.success(list);
    }





    /**
     * 修改单据主表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryDepotHead:edit')")
    @Log(title = "单据主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RyDepotHead ryDepotHead)
    {
        return toAjax(ryDepotHeadService.updateRyDepotHead(ryDepotHead));
    }






    /**
     * 新增单据主表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryDepotHead:add')")
    @Log(title = "单据主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RyDepotHead ryDepotHead)
    {
        return toAjax(ryDepotHeadService.insertRyDepotHead(ryDepotHead));
    }



    /**
     * 删除单据主表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryDepotHead:remove')")
    @Log(title = "单据主表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ryDepotHeadService.deleteRyDepotHeadByIds(ids));
    }
}
